define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/recyclebin' + location.search,
                    restore_url: 'business/recyclebin/restore',
                    destroy_url: 'business/recyclebin/destroy'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __("ID") },
                        { field: 'nickname', title: __('客户名称'), operate: 'LIKE' },
                        { field: 'source.name', title: __('客户来源'), operate: 'LIKE' },
                        { field: 'gender', title: __('性别'), searchList: { "0": __('保密'), "1": __('男'), "2": __('女') }, formatter: Table.api.formatter.normal },
                        { field: 'visit.createtime', title: __('最后一次回访时间'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'visit.content', title: __('回访内容'), operate: 'LIKE', formatter: Table.api.formatter.content },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'business/recyclebin/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'business/recyclebin/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 给表格的按钮绑定点击事件
            $(document).on('click', '.btn-restore', function () {
                // 弹出确认对话框
                Layer.confirm(__('是否确认还原数据'), { icon: 3, title: __('Warning'), shadeClose: true }, function (index) {
                    // 获取当前选中的id
                    var ids = Table.api.selectedids(table)

                    // 发送ajax请求
                    Backend.api.ajax(
                        // 请求地址
                        { url: $.fn.bootstrapTable.defaults.extend.restore_url + `?ids=${ids}` },
                        // 回调函数
                        function () {
                            // 关闭窗口
                            Layer.close(index)

                            // 刷新数据表格
                            table.bootstrapTable('refresh')
                        }
                    )
                })
            })

            // 给表格的按钮绑定点击事件
            $(document).on('click', '.btn-destroy', function () {
                // 弹出确认对话框
                Layer.confirm(__('是否确认还原数据'), { icon: 3, title: __('Warning'), shadeClose: true }, function (index) {
                    // 获取当前选中的id
                    var ids = Table.api.selectedids(table)

                    // 发送ajax请求
                    Backend.api.ajax(
                        // 请求地址
                        { url: $.fn.bootstrapTable.defaults.extend.destroy_url + `?ids=${ids}` },
                        // 回调函数
                        function () {
                            // 关闭窗口
                            Layer.close(index)

                            // 刷新数据表格
                            table.bootstrapTable('refresh')
                        }
                    )
                })
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
